<?php
namespace app\common\logic\admin;
use app\common\model\ImgModel;
use app\common\logic\home\Material;
use app\common\logic\Qiniu;
//从接口得到返回是数据
class Imgs
{
    public function deal($param){
        

        $data_img = $this->getList($param);
        $Material = new Material(); 
        $data_material = $Material->getImgList();
        //$qny_big_path = [];
        foreach ($data_img as $key => $value) {
            if(in_array($value['qny_big_path'], $data_material)){
                ImgModel::where('img_id',$value['img_id'])->update(['status'=>6]);//有用
                //unset($data_img[$key]);
                
            }else{
            	if(!empty($value['qny_big_path'])){
            		echo '已进入删除队列：'.$value['qny_big_path'].'<br/>';
            	}
            	if(!empty($value['qny_path'])){
            		echo '已进入删除队列：'.$value['qny_path'].'<br/>';
            	}
            	ImgModel::where('img_id',$value['img_id'])->update(['status'=>5]);//待删除
            	//$qny_big_path[] = $value['qny_big_path'];
            }
            
        }
        echo "执行成功";
        //$qny_big_path = implode(',', $qny_big_path);
        //halt($qny_big_path);
        /*$BucketManager = $Qiniu->getObject();
        $bucket = $Qiniu->getBucket();

        foreach ($data_img as $key => $value) {
            if(!empty($value['qny_big_path'])){
                //dump($value['qny_big_path']);
                $img = substr($value['qny_big_path'],strpos($value['qny_big_path'],'.com')+5);
                $rs = $BucketManager->delete($bucket,$img);
                if(empty($rs)){
                    echo '删除：'.$value['qny_big_path'].'<br/>';
                }

                $rs = $BucketManager->delete($bucket,$img.'-thumbnail300x300');
                if(empty($rs)){
                    echo '删除：'.$value['qny_big_path'].'-thumbnail300x300'.'<br/>';
                }

                $rs = $BucketManager->delete($bucket,$img.'-thumbnail120x120');
                if(empty($rs)){
                    echo '删除：'.$value['qny_big_path'].'-thumbnail120x120'.'<br/>';
                }
            }
            if(!empty($value['qny_path'])){
                
                $img = substr($value['qny_path'],strpos($value['qny_path'],'.com')+5);
                $rs = $BucketManager->delete($bucket,$img);
                if(empty($rs)){
                    echo '删除：'.$value['qny_path'].'<br/>';
                }
                $rs = $BucketManager->delete($bucket,$img.'-thumbnail300x300');
                if(empty($rs)){
                    echo '删除：'.$value['qny_path'].'-thumbnail300x300'.'<br/>';
                }

                $rs = $BucketManager->delete($bucket,$img.'-thumbnail120x120');
                if(empty($rs)){
                    echo '删除：'.$value['qny_path'].'-thumbnail120x120'.'<br/>';
                }
            }
            $rs = ImgModel::where('img_id',$value['img_id'])->update(['status'=>5]);
            echo $rs.'-'.$value['img_id'];
        }*/
        
    }

    public function deleteImgs(){

    	$Qiniu = new Qiniu();
    	$data_img = ImgModel::where('status',5)->field('qny_path,qny_big_path,img_id,status')->limit(0,100)->select()->toArray();
    	dump($data_img);
    	$BucketManager = $Qiniu->getObject();
        $bucket = $Qiniu->getBucket();

        foreach ($data_img as $key => $value) {
            if(!empty($value['qny_big_path'])){
                //dump($value['qny_big_path']);
                $img = substr($value['qny_big_path'],strpos($value['qny_big_path'],'.com')+5);
                $rs = $BucketManager->delete($bucket,$img);
                if(empty($rs)){
                    echo '删除：'.$value['qny_big_path'].'<br/>';
                }

                $rs = $BucketManager->delete($bucket,$img.'-thumbnail300x300');
                if(empty($rs)){
                    echo '删除：'.$value['qny_big_path'].'-thumbnail300x300'.'<br/>';
                }

                $rs = $BucketManager->delete($bucket,$img.'-thumbnail120x120');
                if(empty($rs)){
                    echo '删除：'.$value['qny_big_path'].'-thumbnail120x120'.'<br/>';
                }
            }
            if(!empty($value['qny_path'])){
                
                $img = substr($value['qny_path'],strpos($value['qny_path'],'.com')+5);
                $rs = $BucketManager->delete($bucket,$img);
                if(empty($rs)){
                    echo '删除：'.$value['qny_path'].'<br/>';
                }
                $rs = $BucketManager->delete($bucket,$img.'-thumbnail300x300');
                if(empty($rs)){
                    echo '删除：'.$value['qny_path'].'-thumbnail300x300'.'<br/>';
                }

                $rs = $BucketManager->delete($bucket,$img.'-thumbnail120x120');
                if(empty($rs)){
                    echo '删除：'.$value['qny_path'].'-thumbnail120x120'.'<br/>';
                }
            }
            $rs = ImgModel::where('img_id',$value['img_id'])->update(['status'=>7]);
            echo $rs.'-'.$value['img_id'].'<br/>';
        }
    }

    public function getList($param)
    {
        $data = ImgModel::where(function($query) use($param){
                            if(!empty($param['start'])){
                                $query->where('create_time','>=',$param['start']);
                            }
                            if(!empty($param['end'])){
                                $query->where('create_time','<=',$param['end']);
                            }
                        })->field('qny_path,qny_big_path,img_id')
                        ->where('status',4)
                        ->limit(0,10000)
                        ->select()
                        ->toArray();

        return $data;
    }

}
	
